package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class activity_broadcast extends AppCompatActivity {
    Button BroadCast_btn ;
    MyBroadCastReceiver myBroadCastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);
        BroadCast_btn = findViewById(R.id.BroadCast_btn);

        registerBroadCastReceiver();
        BroadCast_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent();
                myIntent.setAction("BC_ACTION");
                myIntent.putExtra("BroadCastMessage","发送内容：你点击发送并接收了一个广播");
                sendBroadcast(myIntent);
            }
        });
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(myBroadCastReceiver);
    }

    private void registerBroadCastReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("BC_ACTION");
        myBroadCastReceiver = new MyBroadCastReceiver();
        registerReceiver(myBroadCastReceiver, filter);
    }


}
